const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const pageConfig = require('../page.config.js');

const dist = path.join(__dirname, '../dist/');
const src = path.join(__dirname, '../src/');

let webpackConfig = {
  mode: 'none',
  // 配置入口
  entry: {},
  // 配置出口
  output: {
    path: dist,
    filename: 'static/js/[name].[hash:7].js',
    publicPath: '/protal/'
  },
  resolve: {
    // 配置模块如何解析
    alias: {
      // 创建 import 或 require 的别名，来确保模块引入变得更简单
      layout: path.resolve(__dirname, '../src/layout'),
      pages: path.resolve(__dirname, '../src/pages'),
      images: path.resolve(__dirname, '../src/images'),
      components: path.resolve(__dirname, '../src/components'),
      common: path.resolve(__dirname, '../src/common')
    }
  },
  module: {
    rules: [
      {
        test: /\.(js)$/,
        loader: 'eslint-loader',
        enforce: 'pre',
        include: [src],
        options: {
          formatter: require('eslint-friendly-formatter')
        }
      },
      // html中的img标签
      {
        test: /\.html$/,
        loader: 'html-withimg-loader',
        include: [src]
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [src]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: 'static/img/[name].[hash:7].[ext]'
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: 'static/media/[name].[hash:7].[ext]'
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: 'static/fonts/[name].[hash:7].[ext]'
        }
      }
    ]
  },
  plugins: [
    // 设置每一次build之前先删除dist
    new CleanWebpackPlugin(
      ['dist/*'], // 匹配删除的文件
      {
        root: path.join(__dirname, '../'), // 根目录
        verbose: true, // 开启在控制台输出信息
        dry: false // 启用删除文件
      }
    ),
    // copy文件
    new CopyWebpackPlugin([{
      from: path.resolve(__dirname, '../public/'),
      to: dist
    }]),
    new ProgressBarPlugin()
  ]
};

if (pageConfig && Array.isArray(pageConfig)) {
  pageConfig.map((page) => {
    webpackConfig.entry[page.name] = `${src}/pages/${page.jsEntry}`;
    webpackConfig.plugins.push(
      new HtmlWebpackPlugin({
        filename: `${dist}/${page.name}.html`,
        template: `${src}/pages/${page.html}`,
        inject: true,
        entry: page.name,
        chunks: [page.name],
        inlineSource: '.(js|css)$',
        minify: {
          removeComments: true,
          collapseWhitespace: true,
          removeAttributeQuotes: true
          // more options:
          // https://github.com/kangax/html-minifier#options-quick-reference
        },
        chunksSortMode: 'dependency'
      })
    );
  });
}

module.exports = webpackConfig;
